package com.ktgj.flight.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.flight.domain.Airliner;
import com.ktgj.flight.domain.vo.RevisecabinpriceVo;
import com.ktgj.flight.mapper.AirlinerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.FlightRevisecabinpriceMapper;
import com.ktgj.flight.domain.FlightRevisecabinprice;
import com.ktgj.flight.service.IFlightRevisecabinpriceService;

import javax.servlet.http.HttpServletRequest;


/**
 * 机票产品直降产品政策Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-21
 */
@Service
public class FlightRevisecabinpriceServiceImpl implements IFlightRevisecabinpriceService 
{
    @Autowired
    private FlightRevisecabinpriceMapper flightRevisecabinpriceMapper;

    @Autowired
    private AirlinerMapper airlinerMapper;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询机票产品直降产品政策
     * 
     * @param reviseid 机票产品直降产品政策ID
     * @return 机票产品直降产品政策
     */
    @Override
    public FlightRevisecabinprice selectFlightRevisecabinpriceById(Long reviseid)
    {
        return flightRevisecabinpriceMapper.selectFlightRevisecabinpriceById(reviseid);
    }

    /**
     * 查询机票产品直降产品政策列表
     * 
     * @param flightRevisecabinprice 机票产品直降产品政策
     * @return 机票产品直降产品政策
     */
    @Override
    public List<FlightRevisecabinprice> selectFlightRevisecabinpriceList(FlightRevisecabinprice flightRevisecabinprice)
    {
        return flightRevisecabinpriceMapper.selectFlightRevisecabinpriceList(flightRevisecabinprice);
    }

    @Override
    public List<FlightRevisecabinprice> selectReviseCabinByCustomerIdCard(String cardId)
    {
       return flightRevisecabinpriceMapper.selectReviseCabinByCustomerIdCard(cardId);
    }

    @Override
    public List<FlightRevisecabinprice> queryReviseCabin() {
        return flightRevisecabinpriceMapper.queryReviseCabin();
    }

    /**
     * 新增机票产品直降产品政策
     * 
     * @param flightRevisecabinprice 机票产品直降产品政策
     * @return 结果
     */
    @Override
    public int insertFlightRevisecabinprice(FlightRevisecabinprice flightRevisecabinprice)
    {
        return flightRevisecabinpriceMapper.insertFlightRevisecabinprice(flightRevisecabinprice);
    }

    /**
     * 新增机票产品直降产品政策
     * */
    @Override
    public int insertRevisecabinprice(HttpServletRequest request, FlightRevisecabinprice flightRevisecabinprice) throws Exception {
        String token = request.getHeader("token");
        R<Employee> loginEmployeeByToken = remoteCustomerService.getLoginEmployeeByToken(token);
        if (loginEmployeeByToken.getData() != null){
            flightRevisecabinprice.setEmployeeid(loginEmployeeByToken.getData().getEmployeeId());
        }
        else {
            throw new Exception("登陆已过期");
        }

        if (flightRevisecabinprice.getDepartureList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getDepartureList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setDeparture(flightRevisecabinprice.getDepartureList().get(i));
                }
                else {
                    flightRevisecabinprice.setDeparture(flightRevisecabinprice.getDeparture() + "," + flightRevisecabinprice.getDepartureList().get(i));
                }
            }
        } else
            flightRevisecabinprice.setDeparture("ALL");

        if (flightRevisecabinprice.getArrivalList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getArrivalList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setArrival(flightRevisecabinprice.getArrivalList().get(i));
                }
                else {
                    flightRevisecabinprice.setArrival(flightRevisecabinprice.getArrivalList() + "," + flightRevisecabinprice.getArrivalList().get(i));
                }
            }
        } else
            flightRevisecabinprice.setArrival("ALL");

        if (flightRevisecabinprice.getSuitweekList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getSuitweekList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setSuitweek(flightRevisecabinprice.getSuitweekList().get(i));
                }
                else {
                    flightRevisecabinprice.setSuitweek(flightRevisecabinprice.getSuitweek() + "," + flightRevisecabinprice.getSuitweekList().get(i));
                }
            }
        }

        if (flightRevisecabinprice.getAccessTypeList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getAccessTypeList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setAccessType(flightRevisecabinprice.getAccessTypeList().get(i));
                }
                else {
                    flightRevisecabinprice.setAccessType(flightRevisecabinprice.getAccessType() + "," + flightRevisecabinprice.getAccessTypeList().get(i));
                }
            }
        }

        flightRevisecabinprice.setOperationtime(new Date());
        return flightRevisecabinpriceMapper.insertFlightRevisecabinprice(flightRevisecabinprice);
    }

    /**
     * 修改机票产品直降产品政策
     * 
     * @param flightRevisecabinprice 机票产品直降产品政策
     * @return 结果
     */
    @Override
    public int updateFlightRevisecabinprice(FlightRevisecabinprice flightRevisecabinprice)
    {
        if (flightRevisecabinprice.getDepartureList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getDepartureList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setDeparture(flightRevisecabinprice.getDepartureList().get(i));
                }
                else {
                    flightRevisecabinprice.setDeparture(flightRevisecabinprice.getDeparture() + "," + flightRevisecabinprice.getDepartureList().get(i));
                }
            }
        } else
            flightRevisecabinprice.setDeparture("ALL");

        if (flightRevisecabinprice.getArrivalList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getArrivalList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setArrival(flightRevisecabinprice.getArrivalList().get(i));
                }
                else {
                    flightRevisecabinprice.setArrival(flightRevisecabinprice.getArrivalList() + "," + flightRevisecabinprice.getArrivalList().get(i));
                }
            }
        } else
            flightRevisecabinprice.setArrival("ALL");

        if (flightRevisecabinprice.getSuitweekList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getSuitweekList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setSuitweek(flightRevisecabinprice.getSuitweekList().get(i));
                }
                else {
                    flightRevisecabinprice.setSuitweek(flightRevisecabinprice.getSuitweek() + "," + flightRevisecabinprice.getSuitweekList().get(i));
                }
            }
        }

        if (flightRevisecabinprice.getAccessTypeList().size() > 0){
            for (int i = 0 ; i < flightRevisecabinprice.getAccessTypeList().size() ; i++){
                if (i == 0){
                    flightRevisecabinprice.setAccessType(flightRevisecabinprice.getAccessTypeList().get(i));
                }
                else {
                    flightRevisecabinprice.setAccessType(flightRevisecabinprice.getAccessType() + "," + flightRevisecabinprice.getAccessTypeList().get(i));
                }
            }
        }
        return flightRevisecabinpriceMapper.updateFlightRevisecabinprice(flightRevisecabinprice);
    }

    /**
     * 批量删除机票产品直降产品政策
     * 
     * @param reviseids 需要删除的机票产品直降产品政策ID
     * @return 结果
     */
    @Override
    public int deleteFlightRevisecabinpriceByIds(Long[] reviseids)
    {
        return flightRevisecabinpriceMapper.deleteFlightRevisecabinpriceByIds(reviseids);
    }

    /**
     * 删除机票产品直降产品政策信息
     * 
     * @param reviseid 机票产品直降产品政策ID
     * @return 结果
     */
    @Override
    public int deleteFlightRevisecabinpriceById(Long reviseid)
    {
        return flightRevisecabinpriceMapper.deleteFlightRevisecabinpriceById(reviseid);
    }

    /**
     * 直降产品列表
     * */
    @Override
    public List<FlightRevisecabinprice> selectFlightRevisecabinpriceList(RevisecabinpriceVo revisecabinpriceVo) {
        List<FlightRevisecabinprice> flightRevisecabinprices = flightRevisecabinpriceMapper.flightRevisecabinpriceList(revisecabinpriceVo);
        if (flightRevisecabinprices.size() > 0){
            for (FlightRevisecabinprice flightRevisecabinprice : flightRevisecabinprices) {
                Airliner airliner = airlinerMapper.selectName(flightRevisecabinprice.getAirlinerId());
                if (airliner != null){
                    flightRevisecabinprice.setAirlinerId(airliner.getAirlinerId());
                    flightRevisecabinprice.setAirlinerName(airliner.getAirliner());
                }

                if (flightRevisecabinprice.getAdjusttype() == 1){
                    flightRevisecabinprice.setAdjustTypeName("比例");
                }
                else if (flightRevisecabinprice.getAdjusttype() == 2){
                    flightRevisecabinprice.setAdjustTypeName("金额");
                }

                Map<String, Object> map = new HashMap<>();
                map.put("id", flightRevisecabinprice.getEmployeeid());
                Employee employee = remoteCustomerService.employeeNameList(map);
                if (employee != null){
                    flightRevisecabinprice.setEmployeeName(employee.getEmployeeName());
                }
            }
        }
        return flightRevisecabinprices;
    }
}
